package com.xd.lib;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.xd.lib.animation.AnimActivity;
import com.xd.lib.dialog.DialogActivity;
import com.xd.lib.listview.ListViewActivity;
import com.xd.lib.share.ShareActivity;
import com.xd.lib.textview.TextViewActivity;
import com.xd.lib.tool.ToolActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/6/5.
 */
public class GridViewActivity extends Activity implements AdapterView.OnItemClickListener{

    private GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview_layout);

        int icon = R.mipmap.ic_launcher;

        String[] funcNames = {
                "TextView",
                "Animation",
                "Share",
                "Dialog",
                "Tool",
                "ListView"
        };

        List<Map<String, Object>> data = new ArrayList<>();
        for (int i=0;i<funcNames.length;i++){
            Map<String, Object> item = new HashMap<>();
            item.put("image", icon);
            item.put("text", funcNames[i]);
            data.add(item);
        }

        String[] from = {"image", "text"};
        int[] to = {R.id.gridview_item_image, R.id.gridview_item_text};
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.gridview_item, from, to);

        gridView = (GridView)this.findViewById(R.id.gridview);
        gridView.setAdapter(simpleAdapter);
        gridView.setOnItemClickListener(this);
    }

    /**
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        /**
         * GridView.GetChildCount() -- 当前视窗中的子控件数目
         * GridView.GetCound() -- 所有子控件数目，大于等于GetChildCount
         */
        int index = gridView.getCount() - position;
        Intent intent = null;
        switch (index){
            case 6:
                intent = new Intent(GridViewActivity.this, TextViewActivity.class);
                break;
            case 5:
                intent = new Intent(GridViewActivity.this, AnimActivity.class);
                break;
            case 4:
                intent = new Intent(GridViewActivity.this, ShareActivity.class);
                break;
            case 3:
                intent = new Intent(GridViewActivity.this, DialogActivity.class);
                break;
            case 2:
                intent = new Intent(GridViewActivity.this, ToolActivity.class);
                break;
            case 1:
                intent = new Intent(GridViewActivity.this, ListViewActivity.class);
                break;
        }
        GridViewActivity.this.startActivity(intent);
    }
}
